<?php

namespace app\base\command;


use app\base\model\RedianData;
use app\base\model\RedianTask;
use app\base\service\TaskService;
use think\admin\Command;
use think\console\Input;
use think\console\Output;
/**
 * 小甲鱼视频生成状态查询任务
 * Class SmsNum
 * @package app\common\command
 */
class XiaojiayuRetrieve extends Command
{
    /**
     * 配置指令
     */
    protected function configure()
    {
        $this->setName('xbase:XiaojiayuRetrieve');
        $this->setDescription('XiaojiayuRetrieve');
    }

    protected function execute(Input $input, Output $output)
    {
        $queue_data = $this->queue->data;
        $task_id = $queue_data['task_id'];
        $task_run_log_id = $queue_data['task_run_log_id'];
        $task = RedianTask::mQuery()->where([ "id"=>$task_id])->find();
        $total = RedianData::mQuery()->where(['video_status'=>2,'task_id'=>$task['id'],'task_run_log_id'=>$task_run_log_id])->whereNotNull("video_id")->count();
        $this->setQueueProgress("开始查询视频生成状态{$total}", 0);
        $i = 0;
        while ($i<($total*20)){
            $this->setQueueProgress("第{$i}次查询视频生成状态", 0);
            $this->insetLog(1,$task_id,"查询视频生成状态",$task_run_log_id);
            $res = TaskService::instance()->videoStatusXiaojiayu($task,$task_run_log_id);
            $c = RedianData::mQuery()->where(['video_status'=>2,'task_id'=>$task['id'],'task_run_log_id'=>$task_run_log_id])->whereNotNull("video_id")->count();
            if(empty($c)){
                break;
            }
            $i++;
            sleep(60);
        }
        $num = $this->app->db->name("redian_data")->where(['task_id'=>$task_id,'audio_status'=>3,'task_run_log_id'=>$task_run_log_id])->whereIn('video_status','1,2')->count();
        $status = 1;
        if($task['status']==0){
            $status = 0;
        }
        if($num==0){
            $s = $this->app->db->name("redian_data")->where(['task_id'=>$task_id,'audio_status'=>3,'video_status'=>3,'task_run_log_id'=>$task_run_log_id])->count();
            if($s>0){
                $is_auto_fb = $this->app->db->name("redian_task")->where(['id'=>$task_id])->value('is_auto_fb');
                if($is_auto_fb==1){
                    if(in_array($task['type'],[1,4])){
                        //创建推送数据到设备任务
                        sysqueue($task['title']."推送数据到设备任务", "xbase:TaskPush",  10, $data = [
                            'task_id'=>$task_id,'task_run_log_id'=>$task_run_log_id
                        ], $rscript = 1, $loops = 0);
                    }else{
                        $this->app->db->name("redian_push_schedule")->where(['task_id'=>$task_id])->update(['status'=>1]);
                    }
                    $this->app->db->name("redian_task")->where(['id'=>$task_id])->update([
                        'progress'=>6
                    ]);
                    if($task_run_log_id){
                        $this->app->db->name("task_run_log")->where(["id"=>$task_run_log_id])->update(['progress'=>6]);
                    }

                }else{


                    $this->app->db->name("redian_task")->where(['id'=>$task_id])->update([
                        'progress'=>7,
                        'status'=>$status
                    ]);
                    if($task_run_log_id){
                        $this->app->db->name("task_run_log")->where(["id"=>$task_run_log_id])->update(['progress'=>7]);
                    }
                }

            }else{
                $this->app->db->name("redian_task")->where(['id'=>$task_id])->update([
                    'error_msg'=>"视频生成失败,点击重置按钮重试",
                    'error_status'=>5,'status'=>$status
                ]);
                if($task_run_log_id){
                    $this->app->db->name("task_run_log")->where(["id"=>$task_run_log_id])->update(['result'=>"视频生成失败,点击重置按钮重试",'error_status'=>5]);
                }
            }
        }else{
            $this->app->db->name("redian_task")->where(['id'=>$task_id])->update([
                'error_msg'=>"视频生成失败,点击重置按钮重试",
                'error_status'=>5,'status'=>$status
            ]);
            if($task_run_log_id){
                $this->app->db->name("task_run_log")->where(["id"=>$task_run_log_id])->update(['result'=>"视频生成失败,点击重置按钮重试",'error_status'=>5]);
            }
        }

       // $this->app->db->name("redian_push_schedule")->where(['task_id'=>$task_id])->update(['status'=>1]);
        $this->setQueueSuccess("任务完成");


    }
    public function insetLog($type,$task_id,$msg='',$task_run_log_id=0)
    {
        $this->app->db->name("task_log")->insert([
            'type'=>$type,
            'task_id'=>$task_id,
            'task_run_log_id'=>$task_run_log_id,
            'content'=>$msg
        ]);
    }



}
